package com.cssnj.portal.service.user;

import com.cssnj.portal.domain.common.UserInfo;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

@Service("layoutServicee")
public class LayoutService extends BaseService {

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        switch (jsonRequest.getHandleCode()) {
            case "getUserSet":
                return getUserSet(jsonRequest);
            case "getAllMb":
                return getAllMb(jsonRequest);
            case "getScList":
                return getScList(jsonRequest);
            case "save":
                return save(jsonRequest);
            case "saveMbWz":
                return saveMbWz(jsonRequest);
            case "showTbyy":
                return showTbyy(jsonRequest);
        }
        return null;
    }

    protected JsonResponse getUserSet(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();

        UserInfo userinfo = (UserInfo)jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));

        ArrayList<Object> params = new ArrayList<>() ;
        params.add(userinfo.getSwrySfDm())   ;
        String zxlx_dm = "1"; //默认展现类型 1
        this.queryDao.queryForList("SELECT ZXLX_DM FROM GL_YH_SY WHERE SWRYSF_DM  =? ",params);

        params.clear();
        params.add(userinfo.getSwrySfDm())   ;
        List<Map<String,Object>> list = this.queryDao.queryForList("SELECT ZXLX_DM FROM GL_YH_SY WHERE SWRYSF_DM  =? ",params);

        if(list.size()>0){
            zxlx_dm=(String)list.get(0).get("ZXLX_DM");
        }
        jsonResponse.getRepData().put("zxlx_dm",zxlx_dm);

        String mbid = null; //默认展现类型 1
        String sfjgjcdm = userinfo.getSfJgjcDm();;

        params.clear();
        params.add(userinfo.getSwrySfDm());
        params.add(sfjgjcdm);

        list = this.queryDao.queryForList("  SELECT MB_ID MBID FROM GL_YH_SY_ZXMB T WHERE SWRYSF_DM  =? AND MB_ID IN (SELECT MB_ID  FROM GL_JGJC_ZXMB G  WHERE JGJC_DM=?  OR JGJC_DM='ALL' ) AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID )   ",params);
        if(list.size()>0){
            mbid=(String)list.get(0).get("MBID");
        }

        if(mbid==null){
            params.clear();
            params.add(sfjgjcdm);
            list = this.queryDao.queryForList(" SELECT MB_ID MBID FROM GL_JGJC_ZXMB T WHERE JGJC_DM=? AND ISMR='1'  AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID ) ",params);
            if(list.size()>0){
                mbid=(String)list.get(0).get("MBID");
            }
        }

        if(mbid==null) {
            params.clear();
            list = this.queryDao.queryForList(" SELECT MB_ID MBID FROM GL_JGJC_ZXMB T  WHERE JGJC_DM='ALL' AND ISMR='1' AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID )  ", params);
            if (list.size() > 0) {
                mbid = (String) list.get(0).get("MBID");
            }
        }

        if(mbid==null) {
            mbid="1";
        }

        jsonResponse.getRepData().put("mbid",mbid);

        List<Map<String,Object>> mbList = this.queryDao.queryForList("SELECT MB_ID MBID,MBMC,ROWN,COLN FROM DM_GL_ZXMB WHERE XY_BJ='1' ",new ArrayList<Object>());

        Map<String, Object> sqlparam = new HashMap<String, Object>();
        sqlparam.put("SWRYSFDM", userinfo.getSwrySfDm());
        sqlparam.put("GW_DM", this.getGwParams(userinfo));
        List<Map<String,Object>> bjList =this.queryDao.queryForList(
                     "SELECT A.MB_ID MBID, A.WZ_ID WZID, A.TBYY_ID ,B.TBYY_MC ,B.TBYY_DZ    \n" +
                        "  FROM GL_YH_SY_BJ A, DM_GY_TBYY B               \n" +
                        " WHERE A.SWRYSF_DM = :SWRYSFDM                   \n" +
                        "   AND A.TBYY_ID = B.TBYY_ID(+) " +
                        "   AND A.TBYY_ID NOT IN( " +
                        "       SELECT DISTINCT C.TBYY_ID FROM GL_GW_TBYY C WHERE C.GW_DM NOT IN(:GW_DM) \n"+
                        "   ) \n",sqlparam);

        for(Map<String,Object> map:bjList){
            String mb_id =(String) map.get("MBID");
            for(Map<String,Object> obj:mbList){
                if(mb_id.equals(obj.get("MBID"))){
                    if(obj.get("bjList")==null){
                        obj.put("bjList",new ArrayList<Map<String,Object>>());
                    }
                    ((List<Map<String,Object>>)obj.get("bjList")).add(map);
                    break;
                }
            }
        }
        jsonResponse.getRepData().put("yhmbList",mbList);

        return jsonResponse;

    }


    protected JsonResponse getAllMb(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        ArrayList<Object> params =new ArrayList<Object>();
        UserInfo usrinfo = (UserInfo)jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        params.add(usrinfo.getSfJgjcDm());

        List<Map<String,Object>> mbList =this.queryDao.queryForList("SELECT A.MB_ID MBID,MBMC,ROWN,COLN FROM DM_GL_ZXMB A,GL_JGJC_ZXMB B WHERE A.MB_ID = B.MB_ID AND A.XY_BJ='1' AND (B.JGJC_DM = ? OR B.JGJC_DM='ALL')  ",params);
        List<Map<String,Object>> bjList =this.queryDao.queryForList(
                     "SELECT A.MB_ID  MBID, \n" +
                        "       A.WZ_ID  WZID, \n" +
                        "       A.COLS,        \n" +
                        "       B.TBYY_ID,     \n" +
                        "       B.TBYY_MC,     \n" +
                        "       B.TBYY_DZ,     \n" +
                        "       B.TBYY_HREF    \n" +
                        "  FROM GL_ZXMB_BJ A, DM_GY_TBYY B, GL_JGJC_ZXMB C\n" +
                        " WHERE A.TBYY_ID = B.TBYY_ID(+)\n" +
                        "   AND A.MB_ID = C.MB_ID\n" +
                        "   AND (C.JGJC_DM = ? OR C.JGJC_DM = 'ALL')",params);

        for(Map<String,Object> map:bjList){
            String mbid =(String) map.get("MBID");
            for(Map<String,Object> obj:mbList){
                if(mbid.equals(obj.get("MBID"))){
                    if(obj.get("bjList")==null){
                        obj.put("bjList",new ArrayList<Map<String,Object>>());
                    }
                    ((List<Map<String,Object>>)obj.get("bjList")).add(map);
                    break;
                }
            }
        }
        jsonResponse.getRepData().put("mbList",mbList);

        return jsonResponse;
    }


    protected JsonResponse getScList(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo)jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));

        List<String> params = new ArrayList<String>();
        params.add(usrinfo.getSwrySfDm());
        String sql =
                "SELECT A.GN_ID, B.SCJ_ID, C.GN_MC, C.JDYS, C.HREF\n" +
                        "  FROM GL_GN_FAV A, GL_GN_SCJ B, GL_GN C   \n" +
                        " WHERE A.YH_ID = B.YH_ID  \n" +
                        "   AND A.SCJ_ID =B.SCJ_ID \n"+
                        "   AND A.YH_ID = ?       \n" +
                        "   AND A.GN_ID = C.GN_ID \n" +
                        "   AND C.XY_BJ = '1' \n" ;
        List<Map<String, Object>> favList =  this.queryDao.queryForList(sql,params);
        List<Map<String, Object>> sjcGnList =  this.queryDao.queryForList("SELECT SCJ_ID,SCJ_MC  FROM GL_GN_SCJ WHERE YH_ID=?  ORDER BY PXXH DESC,LR_SJ DESC",params);

        for(int i=0;i<sjcGnList.size();i++){
            String scj_id =sjcGnList.get(i).get("SCJ_ID").toString();
            List<Map<String,Object>> gnlist = new ArrayList<Map<String, Object>>();
            for(int j=0;j<favList.size();j++){
                Map<String, Object> map =favList.get(j);
                if(scj_id.equals(map.get("scj_id").toString())){
                    gnlist.add(map);
                }
            }
            sjcGnList.get(i).put("gnList",gnlist);
        }

        jsonResponse.getRepData().put("sjcGnList",sjcGnList);

        return jsonResponse;
    }

    @Transactional
    protected JsonResponse save(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();
        UserInfo userinfo = (UserInfo)jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String zxlx = (String)jsonRequest.getReqData().get("zxlx");
        String mbid = (String)jsonRequest.getReqData().get("mbid");
        String swrysf_dm = userinfo.getSwrySfDm();
        ArrayList<Object> params = new  ArrayList<Object>();
        params.add(swrysf_dm);
        this.baseDao.getJdbcTemplate().update(" DELETE GL_YH_SY WHERE SWRYSF_DM=? ",params.toArray());
        this.baseDao.getJdbcTemplate().update(" DELETE GL_YH_SY_ZXMB WHERE SWRYSF_DM = ?",params.toArray());

        params.clear();
        params.add(swrysf_dm);
        params.add(zxlx);
        params.add(swrysf_dm);
        this.baseDao.getJdbcTemplate().update(" INSERT INTO GL_YH_SY (SWRYSF_DM,ZXLX_DM,LRRY_ID) VALUES (?,?,?) ",params.toArray());

        params.clear();
        params.add(swrysf_dm);
        params.add(mbid);
        this.baseDao.getJdbcTemplate().update(" INSERT INTO GL_YH_SY_ZXMB (SWRYSF_DM,MB_ID) VALUES(?,?)  ",params.toArray());

        return jsonResponse;
    }

    @Transactional
    protected JsonResponse saveMbWz(JsonRequest jsonRequest) throws SQLException, BaseException {
        JsonResponse jsonResponse = new JsonResponse();
        UserInfo usrinfo = (UserInfo)jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        String wzid = (String)jsonRequest.getReqData().get("wzid");
        String mbid = (String)jsonRequest.getReqData().get("mbid");
        String tbyy_id = (String)jsonRequest.getReqData().get("tbyy_id");
        String swrysfDm = usrinfo.getSwrySfDm();

        ArrayList<Object> params = new ArrayList<Object>();

        params.add(swrysfDm);
        params.add(mbid)    ;
        params.add(wzid)    ;

        this.baseDao.getJdbcTemplate().update(" DELETE GL_YH_SY_BJ WHERE SWRYSF_DM=? AND MB_ID=? AND WZ_ID=?  ",params.toArray());
        params.add(tbyy_id) ;
        this.baseDao.getJdbcTemplate().update(" INSERT INTO GL_YH_SY_BJ(SWRYSF_DM,MB_ID,WZ_ID,TBYY_ID) VALUES (?,?,?,?)   ",params.toArray());
        return jsonResponse;

    }

    protected JsonResponse showTbyy(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();

        UserInfo userinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));

        List<String> gwParams = this.getGwParams(userinfo);
        String wzid= (String)jsonRequest.getReqData().get("wzid");
        String mbid= (String)jsonRequest.getReqData().get("mbid");

        Map<String, Object> sqlparam = new HashMap<String, Object>();
        sqlparam.put("MB_ID", mbid);
        sqlparam.put("WZ_ID", wzid);
        sqlparam.put("GW_DM", gwParams);

        List<Map<String,Object>> tbyyList =this.queryDao.queryForList(
                     " SELECT A.MB_ID, A.WZ_ID, B.TBYY_ID,                \n" +
                        "       B.TBYY_MC, B.TBYY_DZ, B.TBYY_HREF            \n" +
                        "  FROM GL_ZXMB_TBYY A, DM_GY_TBYY B   \n" +
                        " WHERE A.TBYY_ID = B.TBYY_ID                 \n" +
                        "   AND A.XY_BJ = '1'                         \n" +
                        "   AND B.XY_BJ = '1'                         \n" +
                        "   AND A.MB_ID = :MB_ID      \n" +
                        "   AND A.WZ_ID = :WZ_ID      \n" +
                        "   AND B.TBYY_ID NOT IN (    \n" +
                        "           SELECT DISTINCT TBYY_ID FROM GL_GW_TBYY WHERE GW_DM NOT IN (:GW_DM) \n" +
                        "   )  \n " ,sqlparam);
        jsonResponse.getRepData().put("tbyyList",tbyyList);
        return jsonResponse;

    }

    private List<String>  getGwParams(UserInfo userInfo){
        List<Map<String, Object>> gwList = userInfo.getGwlist();
        List<String> gwParams = new ArrayList<String>();
        for (int i = 0; i < gwList.size(); i++) {
            gwParams.add((String) gwList.get(i).get("GW_DM"));
        }
        return gwParams;
    }

}